package com.tcode.service;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;

import com.tcode.bean.UserBean;
import com.tcode.dao.UserDao;
import com.tcode.listener.ContextLoaderListener;
import com.tcode.utils.BeanFactory;
import com.tcode.utils.Page;

public class UserService {
    
	
	UserDao ud = (UserDao) BeanFactory.getBean("UserDao");
//	UserDao ud = new UserDao();
	
	
	
	public boolean getUserCount(UserBean u) throws Exception{
		boolean flag = false;
		if(ud.getUserByUserNameAndPwd(u)!=0){
			flag = true;
		}
		
		return flag;
	}
	
	public UserBean getUserInfo(UserBean u) throws Exception{
		return ud.getUserbeanByUserNameAndPwd(u);
	}
	
	public boolean addUser(UserBean u) throws SQLException{
		boolean flag = false;
		if(ud.insertUser(u)!=0){
			flag = true;
		}
		return flag;
	}
	
	public boolean updateUser(UserBean u)throws Exception{
		boolean flag = false;
		if(ud.modUser(u)!=0){
			flag = true;
		}
		return flag;
	}
	
	public Page<UserBean> getUserList(UserBean u ,int pageNo,int pageSize) throws Exception{
		
		Page<UserBean> page = new Page<UserBean>(pageNo, pageSize);
//		page.setPageNo(pageNo);
		page.setTotalNum(ud.getUserCountBySomething(u));
		List<UserBean> list =ud.getUserList(u, page.getIndexNo(), pageSize);
//		System.out.println(page.getTotalPage());
		page.setList(list);
		return page;
		
	}
	
	public List<UserBean> getUserForExcel() throws SQLException{
		return ud.searchAllUser();
	}

}
